<?php
/**
 * 新浪云计算缓存操作
 * @author Tongle Xu <xutongle@gmail.com> 2012-12-27
 * @copyright Copyright (c) 2003-2103 www.tintsoft.com
 * @version $Id: SAE.php 623 2013-07-29 03:40:03Z 85825770@qq.com $
 */
class Cache_Driver_SAE extends Cache {
	
	/**
	 * 是否对缓存采取压缩存储
	 *
	 * @var int
	 */
	public $compress = 0;
	
	/**
	 * memcache缓存操作句柄
	 *
	 * @var Memcache
	 */
	private $memcache = null;
	
	/**
	 *
	 * @see Cache::init()
	 */
	public function init() {
		parent::init ();
		$this->memcache = memcache_init ();
	}
	
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Cache::setValue()
	 */
	protected function setValue($key, $value, $expire = 0) {
		return memcache_set ( $this->memcache, $key, $this->compress, ( int ) $expire );
	}
	
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Cache::addValue()
	 */
	protected function addValue($key, $value, $expire) {
		return (NULL === memcache_get ( $this->memcache, $key, $this->compress )) ? $this->setValue ( $key, $value, $expire ) : false;
	}
	
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Cache::getValue()
	 */
	protected function getValue($key) {
		$result = memcache_get ( $this->memcache, $key, $this->compress );
		return $result !== NULL ? $result : false;
	}
	
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Cache::deleteValue()
	 */
	protected function deleteValue($key) {
		return memcache_delete ( $this->memcache, $key );
	}
	
	/**
	 * (non-PHPdoc)
	 * 
	 * @see Cache::flushValues()
	 */
	public function flushValues() {
		return memcache_flush ( $this->memcache );
	}
}